<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>flatIterator</title>
</head>

<body>
    <div>打开浏览器控制台查看结果</div>
    <script>
        const flatIterator = function* (iter) {
            for (let item of iter) {
                if (item[Symbol.iterator]) {
                    yield* flatIterator(item);
                } else {
                    yield item;
                }
            }
        };
        const arr = [1, 2, [3, 4], [5, [6, [7], 8]], 9, new Set([10, 11])];
        console.log([...flatIterator(arr)]);; // 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
    </script>
</body>

</html>